<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .box1 {
        width: 100px;
        height: 100px;
        background-color: aqua;
        margin-top: 30px;
        position: absolute;
        left: 0;
      }
      .box2 {
        width: 100px;
        height: 100px;
        background-color: rgb(68, 10, 230);
        margin-top: 150px;
        position: absolute;
        left: 0;
      }
    </style>
  </head>
  <body>
    <button class="btn1">盒子开始移动600</button>
    <button class="btn2">盒子开始移动800</button>
    <div class="box1"></div>
    <div class="box2"></div>
    <script>
      let btn1 = document.querySelector(".btn1");
      let btn2 = document.querySelector(".btn2");
      let box1 = document.querySelector(".box1");
      let box2 = document.querySelector(".box2");
      btn1.onclick = function () {
        animate(box1, 600);
      };
      btn2.onclick = function () {
        animate(box1, 800);
      };

      function animate(obj, target) {
        clearInterval(obj.itme);
        obj.itme = setInterval(function () {
        //   let step = Math.ceil((target - obj.offsetLeft) / 10);
         let step=(target - obj.offsetLeft) / 10
           step>0?Math.ceil(step):Math.floor(step);
          if (obj.offsetLeft == target) {
            clearInterval(obj.itme);
            return;
          }

          obj.style.left = obj.offsetLeft + step + "px";
        }, 15);
      }
    </script>
  </body>
</html>
